#include "Core/String/String.h"
#include "Core/String/StringRef.h"
#include "Core/Time/TimeMeasure.h"
#include "Core/Log/LogManager.h"
#include "Core/Profile/CodeProfile.h"
#include "StringTest.h"

const char * StringTest::GetName() const
{
	return "String Test";
}

error StringTest::Create()
{
	if(Failed(UnitTest::Create()))
			return errFailed;

	return errOK;
}

error StringTest::Destroy()
{
	if(Failed(UnitTest::Destroy()))
		return errFailed;

	return errOK;
}

error StringTest::Execute()
{
	PROFILE(GetName());


	String str1("toto");
	String str2(str1);
	String str3('a');
	String str4;
	String str5 = "Where is toto ?";

	// Copy constructor & operator !=
	if(str2 != "toto")
		return errFailed;

	// operator !=
	if(str2 != str1)
		return errFailed;

	// GetLength
	if( str1.GetLength() != 4)
		return errFailed;

	// GetLength
	str1 += " is in the kitchen. ";
	str1 += str5;
	if( str1.GetLength() != 4+20+str5.GetLength() )
		return errFailed;

	// SubString
	String str6 = str1.SubString(15, 15+7);
	if( str6 != "kitchen")
		return errFailed;

	// SubString
	String str7 = str1.SubString(15, 15+1);
	if( str7 != "k")
		return errFailed;

	// FindFirst
	int iFindFirst = str1.FindFirst(str2);
	if( iFindFirst != 0)
		return errFailed;

	// FindLast
	int iFindLast = str1.FindLast(str2);
	if(iFindLast != 33)
		return errFailed;

	// Replace
	str2.Replace('o', 'a');
	if(str2 != "tata")
		return errFailed;

	// variadic args
	str2.Printf("%d", 4);
	if(str2 != "4")
		return errFailed;

	str2 = "tata";

	StringRef rstr1(str2);
	StringRef rstr2="tata";

	if(rstr1 != rstr2)
		return errFailed;

	return errOK;
}
